home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 109 / EnigmaAmiga109CD.iso / dalla rivista / host contacted / jikes.lha / jikes-1.11 / src / double.h < prev    next >
C/C++ Source or Header  |  2000-01-06  |  6KB  |  218 lines

  1. // $Id: double.h,v 1.6 2000/01/06 06:46:47 lord Exp $
  2. //
  3. // This software is subject to the terms of the IBM Jikes Compiler
  4. // License Agreement available at the following URL:
  5. // http://www.ibm.com/research/jikes.
  6. // Copyright (C) 1996, 1998, International Business Machines Corporation
  7. // and others.  All Rights Reserved.
  8. // You must accept the terms of that agreement to use this software.
  9. //
  10. #ifndef Double_INCLUDED
  11. #define Double_INCLUDED
  12.  
  13. #include "config.h"
  14. #include <math.h>
  15.  
  16. class LongInt;
  17. class IEEEdouble;
  18. class IEEEfloat
  19. {
  20. private:
  21.     union
  22.     {
  23.         float float_value;
  24.         u4 word;
  25.     } value;
  26.  
  27.     enum { BIAS = 127 };
  28.  
  29. public:
  30.  
  31.     u4 Word() { return value.word; }
  32.  
  33.     float FloatValue() { return value.float_value; }
  34.     float FloatRoundedValue();
  35.  
  36.     int Exponent()
  37.     {
  38.         return ((value.word >> 23) & 0x000000FF) - BIAS;
  39.     }
  40.  
  41.     bool IsNaN()
  42.     {
  43.         return (value.word & 0xFF800000) == 0x7F800000 && (value.word & 0x007FFFFF) > 0;
  44.     }
  45.  
  46.     bool IsNegative()
  47.     {
  48.         return (value.word & 0x80000000) == 0x80000000;
  49.     }
  50.  
  51.     bool IsNegativeZero()
  52.     {
  53.         return value.word == 0x80000000;
  54.     }
  55.  
  56.     bool IsPositiveZero()
  57.     {
  58.         return value.word == 0x00000000;
  59.     }
  60.  
  61.     bool IsNegativeInfinity()
  62.     {
  63.         return value.word == 0xFF800000;
  64.     }
  65.  
  66.     bool IsPositiveInfinity()
  67.     {
  68.         return value.word == 0x7F800000;
  69.     }
  70.  
  71.     static inline IEEEfloat NaN()               { return  IEEEfloat(0x7FC00000); }
  72.     static inline IEEEfloat POSITIVE_INFINITY() { return  IEEEfloat(0x7F800000); }
  73.     static inline IEEEfloat NEGATIVE_INFINITY() { return  IEEEfloat(0xFF800000); }
  74.  
  75.     IEEEfloat(float);
  76.     IEEEfloat(u4);
  77.     IEEEfloat(i4);
  78.     IEEEfloat(char *);
  79.     IEEEfloat(IEEEdouble a);
  80.     IEEEfloat() {}
  81.  
  82.     inline int IntValue() { return (int) FloatValue(); }
  83.  
  84.     IEEEfloat  operator+  (IEEEfloat); // binary addition
  85.     IEEEfloat  operator+  ();         // unary plus
  86.     IEEEfloat& operator+= (IEEEfloat); // add and assign
  87.  
  88.     IEEEfloat  operator-  (IEEEfloat); // binary subtraction
  89.     IEEEfloat  operator-  ();         // unary minus
  90.     IEEEfloat& operator-= (IEEEfloat); // subtract and assign
  91.  
  92.     IEEEfloat  operator* (IEEEfloat);  // multiplication
  93.     IEEEfloat& operator*=(IEEEfloat);  // multiply and assign
  94.  
  95.     IEEEfloat  operator/ (IEEEfloat);  // divide
  96.     IEEEfloat& operator/=(IEEEfloat);  // divide and assign
  97.  
  98.     bool      operator== (IEEEfloat); // equal
  99.     bool      operator!= (IEEEfloat); // not equal
  100.  
  101.     bool  operator<  (IEEEfloat); // less-than
  102.     bool  operator>  (IEEEfloat); // greater-than
  103.     bool  operator<= (IEEEfloat); // less-than or equal
  104.     bool  operator>= (IEEEfloat); // greater-than or equal
  105.  
  106.     static void Fmodulus(IEEEfloat, IEEEfloat, IEEEfloat&);
  107.     static void Divide(IEEEfloat, IEEEfloat, IEEEfloat &, IEEEfloat &);
  108. };
  109.  
  110.  
  111. class IEEEdouble
  112. {
  113. private:
  114.     union
  115.     {
  116.         double double_value;
  117.         u4 word[2];
  118.     } value;
  119.  
  120.     enum { BIAS = 1023 };
  121.  
  122. #ifdef WORDS_BIGENDIAN
  123.     u4 &High() { return value.word[0]; }
  124.     u4 &Low()  { return value.word[1]; }
  125. #else
  126.     u4 &Low()  { return value.word[0]; }
  127.     u4 &High() { return value.word[1]; }
  128. #endif
  129.  
  130. public:
  131.  
  132.     u4 HighWord() { return High(); }
  133.     u4 LowWord()  { return Low(); }
  134.  
  135.     double DoubleValue() { return value.double_value; }
  136.     double DoubleRoundedValue();
  137.  
  138.     int Exponent() // Notice that the value returned here is unadjusted
  139.     {
  140.         return ((HighWord() >> 20) & 0x000007FF) - BIAS;
  141.     }
  142.  
  143.     bool IsNaN()
  144.     {
  145.         u4 high = HighWord(),
  146.            low  = LowWord();
  147.         return (high & 0xFFF00000) == 0x7FF00000 && ((high & 0x000FFFFF) > 0 || low > 0);
  148.     }
  149.  
  150.     bool IsNegative()
  151.     {
  152.         return (HighWord() & 0x80000000) == 0x80000000;
  153.     }
  154.  
  155.     bool IsNegativeZero()
  156.     {
  157.         return HighWord() == 0x80000000 && LowWord() == 0x00000000;
  158.     }
  159.  
  160.     bool IsPositiveZero()
  161.     {
  162.         return HighWord() == 0x00000000 && LowWord() == 0x00000000;
  163.     }
  164.  
  165.     bool IsNegativeInfinity()
  166.     {
  167.         return HighWord() == 0xFFF00000 && LowWord() == 0x00000000;
  168.     }
  169.  
  170.     bool IsPositiveInfinity()
  171.     {
  172.         return HighWord() == 0x7FF00000 && LowWord() == 0x00000000;
  173.     }
  174.  
  175.     static IEEEdouble min_long;
  176.  
  177.     static inline IEEEdouble NaN()               { return  IEEEdouble(0x7FF80000, 0x00000000); }
  178.     static inline IEEEdouble POSITIVE_INFINITY() { return  IEEEdouble(0x7FF00000, 0x00000000); }
  179.     static inline IEEEdouble NEGATIVE_INFINITY() { return  IEEEdouble(0xFFF00000, 0x00000000); }
  180.  
  181.     IEEEdouble(LongInt&);
  182.     IEEEdouble(double);
  183.     IEEEdouble(u4, u4);
  184.     IEEEdouble(u4);
  185.     IEEEdouble(IEEEfloat);
  186.     IEEEdouble(i4);
  187.     IEEEdouble(char *);
  188.     IEEEdouble() {}
  189.  
  190.     inline int IntValue() { return (int) DoubleValue(); }
  191.  
  192.     IEEEdouble  operator+  (IEEEdouble); // binary addition
  193.     IEEEdouble  operator+  ();         // unary plus
  194.     IEEEdouble& operator+= (IEEEdouble); // add and assign
  195.  
  196.     IEEEdouble  operator-  (IEEEdouble); // binary subtraction
  197.     IEEEdouble  operator-  ();         // unary minus
  198.     IEEEdouble& operator-= (IEEEdouble); // subtract and assign
  199.  
  200.     IEEEdouble  operator* (IEEEdouble);  // multiplication
  201.     IEEEdouble& operator*=(IEEEdouble);  // multiply and assign
  202.  
  203.     IEEEdouble  operator/ (IEEEdouble);  // divide
  204.     IEEEdouble& operator/=(IEEEdouble);  // divide and assign
  205.  
  206.     bool  operator== (IEEEdouble); // equal
  207.     bool  operator!= (IEEEdouble); // not equal
  208.     bool  operator<  (IEEEdouble); // less-than
  209.     bool  operator>  (IEEEdouble); // greater-than
  210.     bool  operator<= (IEEEdouble); // less-than or equal
  211.     bool  operator>= (IEEEdouble); // greater-than or equal
  212.  
  213.     static void Divide(IEEEdouble, IEEEdouble, IEEEdouble &);
  214.     static void Fmodulus(IEEEdouble, IEEEdouble, IEEEdouble &);
  215. };
  216.  
  217. #endif
  218.